<!DOCTYPE html>
<html>
<head>
<title>2018210222015范吉祥第六周.md</title>
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">

<style>
/* https://github.com/microsoft/vscode/blob/master/extensions/markdown-language-features/media/markdown.css */
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

body {
	font-family: var(--vscode-markdown-font-family, -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif);
	font-size: var(--vscode-markdown-font-size, 14px);
	padding: 0 26px;
	line-height: var(--vscode-markdown-line-height, 22px);
	word-wrap: break-word;
}

#code-csp-warning {
	position: fixed;
	top: 0;
	right: 0;
	color: white;
	margin: 16px;
	text-align: center;
	font-size: 12px;
	font-family: sans-serif;
	background-color:#444444;
	cursor: pointer;
	padding: 6px;
	box-shadow: 1px 1px 1px rgba(0,0,0,.25);
}

#code-csp-warning:hover {
	text-decoration: none;
	background-color:#007acc;
	box-shadow: 2px 2px 2px rgba(0,0,0,.25);
}

body.scrollBeyondLastLine {
	margin-bottom: calc(100vh - 22px);
}

body.showEditorSelection .code-line {
	position: relative;
}

body.showEditorSelection .code-active-line:before,
body.showEditorSelection .code-line:hover:before {
	content: "";
	display: block;
	position: absolute;
	top: 0;
	left: -12px;
	height: 100%;
}

body.showEditorSelection li.code-active-line:before,
body.showEditorSelection li.code-line:hover:before {
	left: -30px;
}

.vscode-light.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(0, 0, 0, 0.15);
}

.vscode-light.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(0, 0, 0, 0.40);
}

.vscode-light.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

.vscode-dark.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(255, 255, 255, 0.4);
}

.vscode-dark.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(255, 255, 255, 0.60);
}

.vscode-dark.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

.vscode-high-contrast.showEditorSelection .code-active-line:before {
	border-left: 3px solid rgba(255, 160, 0, 0.7);
}

.vscode-high-contrast.showEditorSelection .code-line:hover:before {
	border-left: 3px solid rgba(255, 160, 0, 1);
}

.vscode-high-contrast.showEditorSelection .code-line .code-line:hover:before {
	border-left: none;
}

img {
	max-width: 100%;
	max-height: 100%;
}

a {
	text-decoration: none;
}

a:hover {
	text-decoration: underline;
}

a:focus,
input:focus,
select:focus,
textarea:focus {
	outline: 1px solid -webkit-focus-ring-color;
	outline-offset: -1px;
}

hr {
	border: 0;
	height: 2px;
	border-bottom: 2px solid;
}

h1 {
	padding-bottom: 0.3em;
	line-height: 1.2;
	border-bottom-width: 1px;
	border-bottom-style: solid;
}

h1, h2, h3 {
	font-weight: normal;
}

table {
	border-collapse: collapse;
}

table > thead > tr > th {
	text-align: left;
	border-bottom: 1px solid;
}

table > thead > tr > th,
table > thead > tr > td,
table > tbody > tr > th,
table > tbody > tr > td {
	padding: 5px 10px;
}

table > tbody > tr + tr > td {
	border-top: 1px solid;
}

blockquote {
	margin: 0 7px 0 5px;
	padding: 0 16px 0 10px;
	border-left-width: 5px;
	border-left-style: solid;
}

code {
	font-family: Menlo, Monaco, Consolas, "Droid Sans Mono", "Courier New", monospace, "Droid Sans Fallback";
	font-size: 1em;
	line-height: 1.357em;
}

body.wordWrap pre {
	white-space: pre-wrap;
}

pre:not(.hljs),
pre.hljs code > div {
	padding: 16px;
	border-radius: 3px;
	overflow: auto;
}

pre code {
	color: var(--vscode-editor-foreground);
	tab-size: 4;
}

/** Theming */

.vscode-light pre {
	background-color: rgba(220, 220, 220, 0.4);
}

.vscode-dark pre {
	background-color: rgba(10, 10, 10, 0.4);
}

.vscode-high-contrast pre {
	background-color: rgb(0, 0, 0);
}

.vscode-high-contrast h1 {
	border-color: rgb(0, 0, 0);
}

.vscode-light table > thead > tr > th {
	border-color: rgba(0, 0, 0, 0.69);
}

.vscode-dark table > thead > tr > th {
	border-color: rgba(255, 255, 255, 0.69);
}

.vscode-light h1,
.vscode-light hr,
.vscode-light table > tbody > tr + tr > td {
	border-color: rgba(0, 0, 0, 0.18);
}

.vscode-dark h1,
.vscode-dark hr,
.vscode-dark table > tbody > tr + tr > td {
	border-color: rgba(255, 255, 255, 0.18);
}

</style>

<style>
/* Tomorrow Theme */
/* http://jmblog.github.com/color-themes-for-google-code-highlightjs */
/* Original theme - https://github.com/chriskempson/tomorrow-theme */

/* Tomorrow Comment */
.hljs-comment,
.hljs-quote {
	color: #8e908c;
}

/* Tomorrow Red */
.hljs-variable,
.hljs-template-variable,
.hljs-tag,
.hljs-name,
.hljs-selector-id,
.hljs-selector-class,
.hljs-regexp,
.hljs-deletion {
	color: #c82829;
}

/* Tomorrow Orange */
.hljs-number,
.hljs-built_in,
.hljs-builtin-name,
.hljs-literal,
.hljs-type,
.hljs-params,
.hljs-meta,
.hljs-link {
	color: #f5871f;
}

/* Tomorrow Yellow */
.hljs-attribute {
	color: #eab700;
}

/* Tomorrow Green */
.hljs-string,
.hljs-symbol,
.hljs-bullet,
.hljs-addition {
	color: #718c00;
}

/* Tomorrow Blue */
.hljs-title,
.hljs-section {
	color: #4271ae;
}

/* Tomorrow Purple */
.hljs-keyword,
.hljs-selector-tag {
	color: #8959a8;
}

.hljs {
	display: block;
	overflow-x: auto;
	color: #4d4d4c;
	padding: 0.5em;
}

.hljs-emphasis {
	font-style: italic;
}

.hljs-strong {
	font-weight: bold;
}
</style>

<style>
/*
 * Markdown PDF CSS
 */

 body {
	font-family: -apple-system, BlinkMacSystemFont, "Segoe WPC", "Segoe UI", "Ubuntu", "Droid Sans", sans-serif, "Meiryo";
	padding: 0 12px;
}

pre {
	background-color: #f8f8f8;
	border: 1px solid #cccccc;
	border-radius: 3px;
	overflow-x: auto;
	white-space: pre-wrap;
	overflow-wrap: break-word;
}

pre:not(.hljs) {
	padding: 23px;
	line-height: 19px;
}

blockquote {
	background: rgba(127, 127, 127, 0.1);
	border-color: rgba(0, 122, 204, 0.5);
}

.emoji {
	height: 1.4em;
}

code {
	font-size: 14px;
	line-height: 19px;
}

/* for inline code */
:not(pre):not(.hljs) > code {
	color: #C9AE75; /* Change the old color so it seems less like an error */
	font-size: inherit;
}

/* Page Break : use <div class="page"/> to insert page break
-------------------------------------------------------- */
.page {
	page-break-after: always;
}

</style>

<script src="https://unpkg.com/mermaid/dist/mermaid.min.js"></script>
</head>
<body>
  <script>
    mermaid.initialize({
      startOnLoad: true,
      theme: document.body.classList.contains('vscode-dark') || document.body.classList.contains('vscode-high-contrast')
          ? 'dark'
          : 'default'
    });
  </script>
<h2 id="%E7%AC%AC%E4%B8%80%E9%A2%98">第一题</h2>
<p>下面表格中给出了20种啤酒(12盎司)的热量、钠含量、酒精含量和价格数据。根据这4个变量对20种啤酒进行系统聚类分析（样品间距离采用欧式距离，聚类方法采用ward方法），为了去掉量纲的影响，可以用scale函数把数据标准化，然后再聚类分析。</p>
<pre class="hljs"><code><div>x&lt;-read.table(<span class="hljs-string">"clipboard"</span>)
x1&lt;-scale(x)
d&lt;-dist(x1,method = <span class="hljs-string">"euclidean"</span>)
HC&lt;-hclust(d,method = <span class="hljs-string">"ward.D"</span>)
plot(HC)

</div></code></pre>
<p><img src="Rplot3.png" alt="avatar"></p>
<div STYLE="page-break-after: always;"></div>
<h2 id="%E7%AC%AC%E4%BA%8C%E9%A2%98">第二题</h2>
<p>下面表格中给出了2011年全国31个省、直辖市、自治区的城镇居民家庭收入消费性支出的8个主要指标的数据，根据这些数据采用K均值聚类法进行聚类分析，其中k=4。</p>
<pre class="hljs"><code><div>y&lt;-read.table(<span class="hljs-string">"clipboard"</span>)
KM&lt;-kmeans(y,<span class="hljs-number">4</span>,algorithm =<span class="hljs-string">"Hartigan-Wong"</span>)
KM
KM$cluster
</div></code></pre>
<p>结果：</p>
<pre class="hljs"><code><div>&gt; KM
K-means clustering with <span class="hljs-number">4</span> clusters of sizes <span class="hljs-number">13</span>, <span class="hljs-number">4</span>, <span class="hljs-number">3</span>, <span class="hljs-number">11</span>

Cluster means:
        x1       x2       x3       x4       x5       x6        x7       x8
<span class="hljs-number">1</span> <span class="hljs-number">4383.881</span> <span class="hljs-number">1512.278</span> <span class="hljs-number">1122.931</span>  <span class="hljs-number">770.340</span> <span class="hljs-number">1460.059</span> <span class="hljs-number">1228.612</span>  <span class="hljs-number">846.4638</span> <span class="hljs-number">444.0038</span>
<span class="hljs-number">2</span> <span class="hljs-number">7587.390</span> <span class="hljs-number">1965.820</span> <span class="hljs-number">1918.150</span> <span class="hljs-number">1467.118</span> <span class="hljs-number">3672.115</span> <span class="hljs-number">3129.315</span> <span class="hljs-number">1215.3050</span> <span class="hljs-number">988.7275</span>
<span class="hljs-number">3</span> <span class="hljs-number">6419.720</span> <span class="hljs-number">1674.000</span> <span class="hljs-number">1537.673</span> <span class="hljs-number">1182.757</span> <span class="hljs-number">2477.300</span> <span class="hljs-number">2230.183</span> <span class="hljs-number">1050.3667</span> <span class="hljs-number">716.9600</span>
<span class="hljs-number">4</span> <span class="hljs-number">5255.227</span> <span class="hljs-number">1630.774</span> <span class="hljs-number">1316.993</span>  <span class="hljs-number">925.520</span> <span class="hljs-number">1785.396</span> <span class="hljs-number">1541.681</span>  <span class="hljs-number">949.9445</span> <span class="hljs-number">496.3827</span>

Clustering vector:
  北京   天津   河北   山西 内蒙古   辽宁   吉林 黑龙江   上海   江苏   浙江   安徽   福建 
     <span class="hljs-number">2</span>      <span class="hljs-number">3</span>      <span class="hljs-number">1</span>      <span class="hljs-number">1</span>      <span class="hljs-number">4</span>      <span class="hljs-number">4</span>      <span class="hljs-number">1</span>      <span class="hljs-number">1</span>      <span class="hljs-number">2</span>      <span class="hljs-number">3</span>      <span class="hljs-number">2</span>      <span class="hljs-number">4</span>      <span class="hljs-number">3</span> 
  江西   山东   河南   湖北   湖南   广东   广西   海南   重庆   四川   贵州   云南   西藏 
     <span class="hljs-number">1</span>      <span class="hljs-number">4</span>      <span class="hljs-number">1</span>      <span class="hljs-number">4</span>      <span class="hljs-number">4</span>      <span class="hljs-number">2</span>      <span class="hljs-number">4</span>      <span class="hljs-number">4</span>      <span class="hljs-number">4</span>      <span class="hljs-number">4</span>      <span class="hljs-number">1</span>      <span class="hljs-number">1</span>      <span class="hljs-number">1</span> 
  陕西   甘肃   青海   宁夏   新疆 
     <span class="hljs-number">4</span>      <span class="hljs-number">1</span>      <span class="hljs-number">1</span>      <span class="hljs-number">1</span>      <span class="hljs-number">1</span> 

Within cluster sum of squares by cluster:
[<span class="hljs-number">1</span>] <span class="hljs-number">4922082</span> <span class="hljs-number">4673100</span> <span class="hljs-number">1126730</span> <span class="hljs-number">5409421</span>
 (between_SS / total_SS =  <span class="hljs-number">81.2</span> %)

Available components:

[<span class="hljs-number">1</span>] <span class="hljs-string">"cluster"</span>      <span class="hljs-string">"centers"</span>      <span class="hljs-string">"totss"</span>        <span class="hljs-string">"withinss"</span>     <span class="hljs-string">"tot.withinss"</span> <span class="hljs-string">"betweenss"</span>   
[<span class="hljs-number">7</span>] <span class="hljs-string">"size"</span>         <span class="hljs-string">"iter"</span>         <span class="hljs-string">"ifault"</span>      
&gt; KM$cluster
  北京   天津   河北   山西 内蒙古   辽宁   吉林 黑龙江   上海   江苏   浙江   安徽   福建 
     <span class="hljs-number">2</span>      <span class="hljs-number">3</span>      <span class="hljs-number">1</span>      <span class="hljs-number">1</span>      <span class="hljs-number">4</span>      <span class="hljs-number">4</span>      <span class="hljs-number">1</span>      <span class="hljs-number">1</span>      <span class="hljs-number">2</span>      <span class="hljs-number">3</span>      <span class="hljs-number">2</span>      <span class="hljs-number">4</span>      <span class="hljs-number">3</span> 
  江西   山东   河南   湖北   湖南   广东   广西   海南   重庆   四川   贵州   云南   西藏 
     <span class="hljs-number">1</span>      <span class="hljs-number">4</span>      <span class="hljs-number">1</span>      <span class="hljs-number">4</span>      <span class="hljs-number">4</span>      <span class="hljs-number">2</span>      <span class="hljs-number">4</span>      <span class="hljs-number">4</span>      <span class="hljs-number">4</span>      <span class="hljs-number">4</span>      <span class="hljs-number">1</span>      <span class="hljs-number">1</span>      <span class="hljs-number">1</span> 
  陕西   甘肃   青海   宁夏   新疆 
     <span class="hljs-number">4</span>      <span class="hljs-number">1</span>      <span class="hljs-number">1</span>      <span class="hljs-number">1</span>      <span class="hljs-number">1</span>
</div></code></pre>
<p>结果分析：</p>
<table>
<thead>
<tr>
<th></th>
<th style="text-align:center">类别1</th>
<th style="text-align:center">类别2</th>
<th style="text-align:center">类别3</th>
<th style="text-align:center">类别4</th>
</tr>
</thead>
<tbody>
<tr>
<td>样品</td>
<td style="text-align:center">河北，山西，吉林，黑龙江，江西，河南，贵州，云南，西藏，甘肃，青海，宁夏，新疆</td>
<td style="text-align:center">北京，上海，广东，浙江</td>
<td style="text-align:center">天津，江苏，福建</td>
<td style="text-align:center">内蒙古，辽宁，安徽，山东，湖北，湖南，广西，海南，重庆，四川，陕西</td>
</tr>
</tbody>
</table>

</body>
</html>
